package com.book.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.book.domain.DownloadFile;
import com.book.util.DBUtil;

public class DownloadFileDao {

	public void addfile(DownloadFile downloadFile){
	Connection connection=	DBUtil.getConnection();
	PreparedStatement preparedStatement=null;
	String sql="insert into t_download(filename,filerealname,filememory) values(?,?,?)";
	try {
		preparedStatement=connection.prepareStatement(sql);
		preparedStatement.setString(1, downloadFile.getFilename());
		preparedStatement.setString(2, downloadFile.getFilerealname());
		preparedStatement.setString(3, downloadFile.getFilememory());
		preparedStatement.executeUpdate();
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
	}
	public List<DownloadFile> listAllFile(){
		List<DownloadFile> downloadFilelist=new ArrayList<DownloadFile>();
	Connection connection=	DBUtil.getConnection();
	String sql="select  * from t_download";
	PreparedStatement preparedStatement=null;
	ResultSet resultSet=null;
	try {
	
		preparedStatement=	connection.prepareStatement(sql);
	    resultSet=	preparedStatement.executeQuery();
	    while (resultSet.next()) {
			DownloadFile downloadFile=new DownloadFile(resultSet.getInt("id"),resultSet.getString("filename"),resultSet.getString("filerealname"),resultSet.getString("filememory"));
		    downloadFilelist.add(downloadFile);
	    }
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		DBUtil.closeConnection(preparedStatement, resultSet, connection);
	}
		return downloadFilelist;
	}
	public DownloadFile selectDownloadFile(DownloadFile downloadFile){
		List<DownloadFile> downloadFilelist=new ArrayList<DownloadFile>();
		Connection connection=	DBUtil.getConnection();
		String sql="select  * from t_download where id=?";
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		DownloadFile downloadFile1 = null;
		try {
			
			preparedStatement=	connection.prepareStatement(sql);
			preparedStatement.setInt(1, downloadFile.getId());
		    resultSet=	preparedStatement.executeQuery();
		    while (resultSet.next()) {
			    downloadFile1=new DownloadFile(resultSet.getInt("id"),resultSet.getString("filename"),resultSet.getString("filerealname"),resultSet.getString("filememory"));
			    
		    }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeConnection(preparedStatement, resultSet, connection);
		}
		return downloadFile1;
	}
}
